package com.sinkdream.cms.controller;

import com.sinkdream.cms.model.News;
import com.sinkdream.cms.service.NewsService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/newsManager")
public class NewsController {
    @Autowired
    private NewsService service;

    /**
    新增新闻功能
     */
    @RequestMapping("/publish")
    public String addNews(News news ,@RequestParam("position") String position){
        if(position.equals("top")){
            news.setNewsTop(true);
            news.setNewsBannerPosition("none");
            news.setNewsIsBanner(false);
        }else if(position.equals("banner")){
            news.setNewsIsBanner(true);
            news.setNewsTop(false);
        }else{
            news.setNewsIsBanner(false);
            news.setNewsTop(false);
            news.setNewsBannerPosition("none");
        }
        service.publishNews(news);
        return "success";
    }

/**
 * 新闻列表展示功能
 */
    @RequestMapping("/getNewsList")
    public List<News> getNewsList(){
       List<News> newsList =  service.getNewsList();
        return newsList;
    }

    /**
     * 修改新闻之前查询新闻的所有内容
     */
    @RequestMapping("/getNews")
    public News getNews(@RequestParam("newsId")int id){
        News news = service.getNewsById(id);
        return news;
    }

    /**
     * 删除新闻
     */
    @RequestMapping("/deleteNews")
    public void deleteNews(@RequestParam("newsId") int newsId){
        service.deleteById(newsId);
    }

    /**
     * 更新新闻
     */
    public String updateNews(News news,@RequestParam("position") String position){
        if(position.equals("top")){
            news.setNewsTop(true);
            news.setNewsBannerPosition("none");
            news.setNewsIsBanner(false);
        }else if(position.equals("banner")){
            news.setNewsIsBanner(true);
            news.setNewsTop(false);
        }else{
            news.setNewsIsBanner(false);
            news.setNewsTop(false);
            news.setNewsBannerPosition("none");
        }
        return "success";
    }

    /**
     * 新闻搜索功能(动态搜索)
     */
    public List<News> dynamicSearch(@RequestParam("publisher")String publisher, @RequestParam("newsType")Integer newsType,@RequestParam("newsCatalog")String newsCatalog){
        if (publisher == null || publisher.length()<=0){

        }
        return null;
    }


}
